﻿using CPPEI.Coolzon.ServiceProxy.DataServiceRef;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.Coolzon.UI.DataTask
{
    /// <summary>
    /// 静态数据加载器，
    /// 从方案目录里读取静态数据文件，并返回
    /// </summary>
    public static class StaticDataSourceManager
    {
        /// <summary>
        /// 从数据源文件的内容字符串中加载
        /// </summary>
        /// <param name="dataSourceContent">内容</param>
        /// <returns></returns>
        public static DataResult LoadDataFromContent(string dataSourceContent)
        {
            object result = null;

            if (!string.IsNullOrEmpty(dataSourceContent))
            {
                using (XmlTextReader xmlReader = new XmlTextReader(dataSourceContent, XmlNodeType.Document, null))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(DataResult));
                    result = xmlSerializer.Deserialize(xmlReader);
                }
            }
            return result as DataResult;
        }

        /// <summary>
        /// 读取数据(从方案目录里读取)
        /// </summary>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public static DataResult LoadDataFromSolution(string dataSource)
        {
            string staticDataFilePath = SolutionResourceManager.GetStaticDataFilePath(dataSource);
            return LoadDataFromFile(staticDataFilePath);
        }

        /// <summary>
        /// 读取数据(从指定的文件里读取)
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static DataResult LoadDataFromFile(string filePath)
        {
            object result = null;

            if (File.Exists(filePath))
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(DataResult));
                    result = xmlSerializer.Deserialize(reader);
                }
            }

            return result as DataResult;
        }

        /// <summary>
        /// 读取文件中的内容
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string LoadDataContentFromFile(string filePath)
        {
            return File.ReadAllText(filePath);
        }


        /// <summary>
        /// 将数据保存(保存到方案目录)
        /// </summary>
        /// <param name="dataResult"></param>
        public static void SaveDataToSolution(DataResult dataResult)
        {
            string staticDataFilePath = SolutionResourceManager.GetStaticDataFilePath(dataResult.Name);
            SaveDataToFile(dataResult, staticDataFilePath);
        }

        /// <summary>
        /// 将数据保存（保存到指定的文件)
        /// </summary>
        /// <param name="dataResult"></param>
        /// <param name="filePath"></param>
        public static void SaveDataToFile(DataResult dataResult, string filePath)
        {
            if (!string.IsNullOrWhiteSpace(filePath) && dataResult != null)
            {
                Type type = dataResult.GetType();
                
                FileInfo fileInfo = new FileInfo(filePath);
                if(fileInfo.Directory!= null)
                {
                    fileInfo.Directory.Create();
                }

                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                    xmlSerializer.Serialize(writer, dataResult);
                }
            }
        }
        public static void SaveDataToFile(string content, string filePath)
        {
            if (!string.IsNullOrWhiteSpace(filePath) && !string.IsNullOrEmpty(content))
            {

                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Directory != null)
                {
                    fileInfo.Directory.Create();
                }
                File.WriteAllText(filePath, content);
            }
        }

        /// <summary>
        /// 将数据保存到字符串格式
        /// </summary>
        /// <param name="dataResult"></param>
        /// <returns></returns>
        public static string SaveDataToString(DataResult dataResult)
        {
            if (dataResult != null)
            {
                Type type = dataResult.GetType();

                using (MemoryStream stream = new MemoryStream())
                {
                    StreamWriter writer = new StreamWriter(stream);
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                    xmlSerializer.Serialize(writer, dataResult);

                    stream.Seek(0, SeekOrigin.Begin);
                    StreamReader reader = new StreamReader(stream);
                    string content = reader.ReadToEnd();
                    reader.Close();
                    writer.Close();

                    return content;
                }
            }
            return null;
        }
    }
}
